//
//  BaseViewController.swift
//  Kool Car
//
//  Created by huangweiqiang on 2021/12/14.
//

import UIKit

class BaseViewController: UIViewController {
    var navBar = WRCustomNavigationBar.CustomNavigationBar()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        if #available(iOS 11.0, *) {
            self.automaticallyAdjustsScrollViewInsets = false//在iOS11 中, UIViewController的automaticallyAdjustsScrollViewInsets属性已经不再使用
        }else{
            self.automaticallyAdjustsScrollViewInsets = false
        }
        let vv = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        self.view.addSubview(vv)
        self.extendedLayoutIncludesOpaqueBars = false
        self.navigationController?.navigationBar.isHidden = true
        self.navigationController?.navigationBar.isTranslucent = false
        self.setupNavBar()

    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        if #available(iOS 13.0, *) {
            return .darkContent
        } else {
            // Fallback on earlier versions
            return UIStatusBarStyle.default
        }
    }
    
    func setupNavBar()  {
        view.addSubview(navBar)
        // 设置自定义导航栏背景图片
        //navBar.barBackgroundImage = UIImage(named: "millcolorGrad")
        // 设置自定义导航栏背景颜色
        navBar.barBackgroundColor = Theme.whiteColor
        // 设置自定义导航栏标题颜色
        navBar.titleLabelColor = UIColor.init(hex: 0x000000)
        // 设置自定义导航栏左右按钮字体颜色
        navBar.wr_setTintColor(color: UIColor.init(hex: 0x333333))
        if self.navigationController?.childViewControllers.count != 1 {
            navBar.wr_setLeftButton(image: UIImage.init(named: "icon_nav_b_back")!)
            navBar.onClickLeftButton = {
                self.backAction()
            }
        }
    }
    
    //设置navibar背景为白色
    func setCustomNavibarBgColor ()  {
        self.navBar.wr_setBackgroundAlpha(alpha: 1)
        self.navBar.barBackgroundImage = UIImage.image(withColor: UIColor.white)
        navBar.backgroundColor = Theme.mainColor
        navBar.titleLabelColor = Theme.titleLabColor
    }
    
    ///设置导航栏背景色
    func setNavibarBgColor(bgColor: UIColor)  {
        self.navBar.wr_setBackgroundAlpha(alpha: 1)
        self.navBar.barBackgroundImage = UIImage.image(withColor: bgColor)
        navBar.backgroundColor = bgColor
        navBar.titleLabelColor = Theme.titleLabColor
    }
    
    //框架返回
    private func back() {
        _ = navigationController?.popViewController(animated: true)
    }
    
    //自定义返回
    @objc func backAction()  {
        self.navigationController?.popViewController(animated: true)
    }
}
